//
//  CLTouchableView.swift
//  clreader
//
//  Created by colin on 2019/2/26.
//  Copyright © 2019 colin. All rights reserved.
//

import UIKit

class CLTouchableView: UIView {

    private lazy var cellMaskView = UIView()
    private var hidding = false
    
    private func showMask() {
        
        self.cellMaskView.backgroundColor = UIColor(hex: "#000000",alpha: 0.2)
        self.cellMaskView.alpha = 0
        self.addSubview(self.cellMaskView)
        self.cellMaskView.frame = self.bounds
        
        UIView.animate(withDuration: 0.2, animations: { [weak self] in
            self?.cellMaskView.alpha = 1.0
        })
    }
    
    func hideMask() {
        
        self.hidding = true
        UIView.animate(withDuration: 0.3, animations: { [weak self] in
            self?.cellMaskView.alpha = 0
            }, completion: { [weak self] (finish) in
                self?.hidding = false
                self?.cellMaskView.removeFromSuperview()
        })
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        self.showMask()
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesMoved(touches, with: event)
        if !self.hidding {
            //hideMask()
        }
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        if !self.hidding {
            self.hideMask()
        }
    }
    
    override func touchesCancelled(_ touches: Set<UITouch>?, with event: UIEvent?) {
        super.touchesCancelled(touches!, with: event)
        if !self.hidding {
            self.hideMask()
        }
    }

}
